package com.cuit.jifver.weibo.activity;


import com.cuit.jifver.weibo.data.ConfigUtil;
import com.cuit.jifver.weibo.data.Weibo;
import com.cuit.jifver.weibo.net.AccessToken;
import com.cuit.jifver.weibo.util.Constants;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;



/**
 * 登录选择界面 1.根据不同的按钮点击初始化数据
 * 
 * @author bywyu
 */
public class MainAct extends Activity implements OnClickListener
{
	private static int MODE = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE;
	private final String LOGTAG = "MainAct";
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shared_main);
		
		Button sinaBtn = (Button) findViewById(R.id.sinaBtn);
		sinaBtn.setOnClickListener(this);
		sinaBtn.setTag(ConfigUtil.SINAW);
		
		Button qqBtn = (Button) findViewById(R.id.qqBtn);
		qqBtn.setOnClickListener(this);
		qqBtn.setTag(ConfigUtil.QQW);
		
		Button sohuBtn = (Button) findViewById(R.id.sohuBtn);
		sohuBtn.setOnClickListener(this);
		sohuBtn.setTag(ConfigUtil.SOHUW);
		
		Button wangyiBtn = (Button) findViewById(R.id.wangyiBtn);
		wangyiBtn.setOnClickListener(this);
		wangyiBtn.setTag(ConfigUtil.WANGYIW);
	}
	
	@Override
	public void onClick(View v) {
		
		// 初始化ConfigUtil信息
		ConfigUtil conf = ConfigUtil.getInstance();
		String curWeibo = String.valueOf(v.getTag());
		conf.setCurWeibo(curWeibo);
		
		Weibo weibo = Weibo.getInstance();
		
		if (curWeibo.equals(ConfigUtil.SINAW)) {
			conf.initSinaData();
		}
		else if (curWeibo.equals(ConfigUtil.QQW)) {
			conf.initQqData();
		}
		else if (curWeibo.equals(ConfigUtil.SOHUW)) {
			conf.initSohuData();
		}
		else if (curWeibo.equals(ConfigUtil.WANGYIW)) {
			conf.initWangyiData();
		}
		
		weibo.setupConsumerConfig(conf.getAppKey(), conf.getAppSecret());
		weibo.setupUrlConfig(conf.getServer_url(), conf.getRequest_token_url(), conf.getAuthoriz_token_url(), conf.getAccess_token_url());
		
		if (!userAuthorized())
		{
			Intent intent = new Intent(MainAct.this, AuthorizationAct.class);
			startActivity(intent);
		}
		else 
		{
			Intent intent = new Intent(MainAct.this, MainListAct.class);
			startActivity(intent);
		}
		
	}
	
	private boolean userAuthorized()
	{
	    SharedPreferences sharedPreferences = getSharedPreferences(Constants.oauth.OAUTH_FILE_NAME, MODE);
	    String accessToken = sharedPreferences.getString(ConfigUtil.access_token_name, null);
	    String accessTokenSecret = sharedPreferences.getString(ConfigUtil.access_token_secret_name, null);
	    if (accessToken != null && accessTokenSecret != null)
		{
			AccessToken accessToken2 = new AccessToken(accessToken, accessTokenSecret);
			Weibo weibo = Weibo.getInstance();
			weibo.setAccessToken(accessToken2);
			return true;
		}
		
		
		return false;
	}
}
